package fucai.com.fucai.task;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import java.io.IOException;

import fucai.com.fucai.initData.InitDataDB;
import fucai.com.fucai.ui.NiuFuCai;

/**
 * @说 明：这里是开线程把txt文件复制到系统中
 * @项目名称：fucai
 * @包 名： fucai.com.fucai
 * @类 名： Fucai
 * @创 建人： chenshuangniu
 * @创建时间：2015/12/11 23:31
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */

public class CopyTxttoFileThread extends Thread {
    private Context context;
    private Handler handler;
    private String txtName;
    private int txtRaw;

    public CopyTxttoFileThread(Context context, Handler handler, String txtName, int txtRaw) {
        this.handler = handler;
        this.txtRaw = txtRaw;
        this.txtName = txtName;
        this.context = context;
    }

    @Override
    public void run() {
        super.run();
        handler.post(new Runnable() {
            InitDataDB initDataDB = new InitDataDB(context, txtName, txtRaw);

            @Override
            public void run() {
                if (null != handler) {
                    boolean dbExist = initDataDB.checkDataBase();
                    if (dbExist) {
                        Log.i("tag", "The database is exist.");
                        handler.sendMessage(handler.obtainMessage(NiuFuCai.COPY_IS_FAILE_TXT, " 可以共用"));
                    } else {
                        try {
                            initDataDB.copyDataBase();
                        } catch (IOException e) {
                            throw new Error("Error copying database");
                        }
                        handler.sendMessage(handler.obtainMessage(NiuFuCai.COPY_IS_OK_TXT));
                    }
                }
            }
        });
    }
}
